/*
 * Copyright 2011-2017 JacobLee007
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.honeybees.intellij.javadocs.generator.impl;

import java.util.Map;

import com.honeybees.intellij.javadocs.model.JavaDoc;
import com.honeybees.intellij.javadocs.model.settings.JavaDocSettings;
import com.honeybees.intellij.javadocs.model.settings.Level;
import com.honeybees.intellij.javadocs.util.JavaDocUtils;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiClass;
import freemarker.template.Template;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * The type Class java doc generator.
 *
 * @author Sergey Timofiychuk
 */
public class ClassJavaDocGenerator extends AbstractJavaDocGenerator<PsiClass> {

    /**
     * Instantiates a new Class java doc generator.
     *
     * @param project the Project
     */
    public ClassJavaDocGenerator(@NotNull Project project) {
        super(project);
    }

    @Nullable
    @Override
    protected JavaDoc generateJavaDoc(@NotNull PsiClass element) {
        JavaDocSettings configuration = getSettings().getConfiguration();
        if ((configuration != null && !configuration.getGeneralSettings().getLevels().contains(Level.TYPE)) ||
                !shouldGenerate(element.getModifierList())) {
            return null;
        }
        Template template = getDocTemplateManager().getClassTemplate(element);
        Map<String, Object> params = getDefaultParameters(element);
        if (!configuration.getGeneralSettings().isSplitClassName()) {
            params.put("name", element.getName());
        }
        String javaDocText = getDocTemplateProcessor().merge(template, params);
        return JavaDocUtils.toJavaDoc(javaDocText, getPsiElementFactory());
    }

}
